//
//  AppDelegate.m
//

#import "AppDelegate.h"

#import "JXLocalPushManager.h"
#import "JXLoginViewController.h"
#import "JXTabBarController.h"

#import "JXMcsChatViewController.h"

@interface AppDelegate ()

@property(strong, nonatomic) UITabBarController *rootVC;

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
        didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    NSString *appkey = nil; // 此处设置自己的appkey，demo中使用用户输入的key，所以这里为nil
    [sClient clientApplication:application
            didFinishLaunchingWithOptions:launchOptions
                                   appkey:appkey
                             apnsCertName:nil];

    [[JXLocalPushManager sharedInstance] registerLocalNotification:application];
    [self setupDefaultUI];

    _rootVC = [[JXTabBarController alloc] init];
    self.window.rootViewController = _rootVC;
    [self.window makeKeyAndVisible];
    [self showLoginWindow];
    return YES;
}

- (void)setupDefaultUI {
    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
    shadow.shadowOffset = CGSizeMake(0, 0);
    [[UINavigationBar appearance] setTitleTextAttributes:@{
        NSForegroundColorAttributeName : [UIColor whiteColor],
        NSFontAttributeName : [UIFont boldSystemFontOfSize:20.0],
        NSShadowAttributeName : shadow
    }];
}

- (void)showLoginWindow {
    if (!self.loginWindow) {
        self.loginWindow = [[UIWindow alloc] initWithFrame:self.window.frame];
        self.loginWindow.top = self.window.height;
        UINavigationController *rootNav = [[UINavigationController alloc]
                initWithRootViewController:[[JXLoginViewController alloc] init]];
        self.loginWindow.rootViewController = rootNav;
        self.loginWindow.windowLevel = UIWindowLevelNormal + 1;
        self.loginWindow.backgroundColor = [UIColor whiteColor];
    }
    [self.loginWindow makeKeyAndVisible];
    self.loginWindow.hidden = NO;

    [UIView beginAnimations:@"EaseIn" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.4];
    self.loginWindow.frame = self.window.frame;
    [UIView commitAnimations];
    [_rootVC setSelectedIndex:0];
}

- (void)application:(UIApplication *)application
        didReceiveLocalNotification:(UILocalNotification *)notification {
    [[JXLocalPushManager sharedInstance] didReceiveLocalNotification:notification];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for
    // certain types of temporary interruptions (such as an incoming phone call or SMS message) or
    // when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame
    // rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store
    // enough application state information to restore your application to its current state in case
    // it is terminated later.
    // If your application supports background execution, this method is called instead of
    // applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo
    // many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive.
    // If the application was previously in the background, optionally refresh the user interface.
    [[JXLocalPushManager sharedInstance] resetApplicationIconBadgeNumber:application];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also
    // applicationDidEnterBackground:.
}

@end
